﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Text.RegularExpressions;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.Office.Server;

namespace LifeInSharePoint.Common
{
    public static class SPTwitterHelper
    {
        public static String AddWebAndTwitterLinks(string input)
        {
            string strWebLinks = @"(^|[\n ])([\w]+?://[\w]+[^ \n\r\t< ]*)";
            string strWebLinksWWW = @"(^|[\n ])((www|ftp)\.[^ \t\n\r< ]*)";
            string strTwitterNames = @"@(\w+)";
            input = Regex.Replace(input, strWebLinks,
                " <a href=\"$2\" target=\"_blank\">$2</a>");
            input = Regex.Replace(input, strWebLinksWWW,
                " <a href=\"http://$2\" target=\"_blank\">$2</a>");
            input = Regex.Replace(input, strTwitterNames,
                "<a href=\"/my/personal/$1\" target=\"_blank\">@$1</a>");

            return input;
        }

        public static string GetUserDetails(SPListItem item)
        {
            string CurrentUserLink = string.Empty;

            //Get Current User Details
            SPUser currentUser = GetSPUser(item, "Created By");
            string CurrentUsersName = currentUser.Name;

            CurrentUserLink = string.Format("<a href='{0}'>{0}</a>", CurrentUsersName);

            return CurrentUserLink;
        }


        public static string GetUserProfilePicture(SPListItem item)
        {
            //To Complete
            string Url = string.Empty;
            try
            {
                //get current profile manager
                UserProfileManager objUserProfileManager = new UserProfileManager(SPServiceContext.GetContext(SPContext.Current.Site));
                //get current users profile
                UserProfile profile = objUserProfileManager.GetUserProfile(SPUserHelper.GetSPUserObject(item, "Created By").LoginName);
                //get user image URL
                Url = profile[PropertyConstants.PictureUrl].ToString().Replace("_MThumb.jpg","_SThumb.jpg");

            }
            catch (Exception ex)
            {
                Url = "/_layouts/images/O14_person_placeHolder_32.png";
            }

            return Url;
        }

        public static SPUser GetSPUser(SPListItem item, string key)
        {
            SPFieldUser field = item.Fields[key] as SPFieldUser;

            if (field != null && item[key] != null)
            {
                SPFieldUserValue fieldValue = field.GetFieldValue(item[key].ToString()) as SPFieldUserValue;
                if (fieldValue != null)
                {
                    return fieldValue.User;
                }
            }
            return null;
        }

        public static string GetDifferenceDate(DateTime TweetDate, DateTime NowDate)
        {
            if (DateTime.Compare(NowDate, TweetDate) >= 0)
            {
                string TimeFormat = string.Empty;
                TimeSpan ts = NowDate.Subtract(TweetDate);
                //Default
                TimeFormat = string.Format("{0} seconds ago", ts.Seconds);
                if (ts.Days > 0)
                {
                    if (ts.Days < 2)
                    {
                        TimeFormat = "Yesterday";
                    }
                    else
                    {
                        TimeFormat = string.Format("{0} days ago", ts.Days);
                    }
                }
                if (ts.Days < 1 && ts.Hours > 0)
                {
                    if (ts.Hours < 2)
                    {
                        TimeFormat = "About an hour ago";
                    }
                    else
                    {
                        TimeFormat = string.Format("{0} hours ago ", ts.Hours);
                    }
                }
                if (ts.Days < 1 && ts.Hours < 1 && ts.Minutes > 0)
                {
                    TimeFormat = string.Format("{0} minutes ago", ts.Minutes);
                }
                if (ts.Days < 1 && ts.Hours < 1 && ts.Minutes < 1 && ts.Seconds > 0)
                {
                    TimeFormat = string.Format("{0} seconds ago", ts.Seconds);
                }
                return TimeFormat;

            }
            else
                return "Not valid";
        }
    }
}
